<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<title>记忆</title>
		<meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no">
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">
		<link rel="stylesheet" href="../css/mui.min.css">
		<link rel="stylesheet" href="../css/icons-extra.css" type="text/css"/>
		<style>
			html,
			body {
				background-color: #efeff4;
			}
		</style>
	</head>

	<body>
		<header id="header-title" class="mui-bar mui-bar-nav" style="background-color: #12b8ec;">
			<!--foffff-->
			<h1 id="title" class="mui-title" style="color: white;">首页</h1>
			<a id="saoyisao" class="mui-icon-extra mui-icon-extra-sweep mui-pull-right" style="line-height: 46px;"></a>
		</header>
		<nav class="mui-bar mui-bar-tab">
			<a id="defaultTab" class="mui-tab-item mui-active" href="index.html">
				<span class="mui-icon mui-icon-home"></span>
				<span class="mui-tab-label">首页</span>
			</a>
			<a id="chart-list" class="mui-tab-item" href="chat-list.html">
				<span class="mui-icon mui-icon-chat"><span class="mui-badge">9</span></span>
				<span class="mui-tab-label">消息</span>
			</a>
			<a class="mui-tab-item" href="contact-list.html">
				<span class="mui-icon mui-icon-contact"></span>
				<span class="mui-tab-label">通讯录</span>
			</a>
			<a class="mui-tab-item" href="setting.html">
				<span class="mui-icon mui-icon-gear"></span>
				<span class="mui-tab-label">设置</span>
			</a>
		</nav>
		<span id='account' style="display: none;"></span>
		<script src="../libs/mui.min.js"></script>
		<script src="../libs/app.js"></script>
		<script type="text/javascript" charset="utf-8">
			//mui初始化
			mui.init({
				swipeBack: false
			});
			var settings = app.getSettings();
			var account = document.getElementById('account');
			window.addEventListener('show', function() {
				var state = app.getState();
				account.innerText = state.account;
			}, false);
			var subpages = ['index.html', 'chat-list.html', 'contact-list.html', 'setting.html'];
			var subpage_style = {
				top: '45px',
				bottom: '51px'
			};

			var aniShow = {};

			//创建子页面，首个选项卡页面显示，其它均隐藏；
			mui.plusReady(function() {
				mui.oldBack = mui.back;
				var backButtonPress = 0;
				mui.back = function(event) {
					backButtonPress++;
					if(backButtonPress > 1) {
						plus.runtime.quit();
					} else {
						plus.nativeUI.toast('再按一次退出应用');
					}
					setTimeout(function() {
						backButtonPress = 0;
					}, 1000);
					return false;
				};
				var self = plus.webview.currentWebview();
				for(var i = 0; i < 4; i++) {
					var temp = {};
					var sub = plus.webview.create(subpages[i], subpages[i], subpage_style);
					if(i > 0) {
						sub.hide();
					} else {
						temp[subpages[i]] = "true";
						mui.extend(aniShow, temp);
					}
					self.append(sub);
				}
				//push();
			});
			//当前激活选项
			var activeTab = subpages[0];
			var title = document.getElementById("title");
			//选项卡点击事件
			mui('.mui-bar-tab').on('tap', 'a', function(e) {
				var targetTab = this.getAttribute('href');
				if(targetTab == activeTab) {
					return;
				}
				//更换标题
				title.innerHTML = this.querySelector('.mui-tab-label').innerHTML;
				//显示目标选项卡
				//若为iOS平台或非首次显示，则直接显示
				if(mui.os.ios || aniShow[targetTab]) {
					plus.webview.show(targetTab);
				} else {
					//否则，使用fade-in动画，且保存变量
					var temp = {};
					temp[targetTab] = "true";
					mui.extend(aniShow, temp);
					plus.webview.show(targetTab, "fade-in", 300);
				}
				//隐藏当前;
				plus.webview.hide(activeTab);
				//更改当前活跃的选项卡
				activeTab = targetTab;
			});
			//自定义事件，模拟点击“首页选项卡”
			document.addEventListener('gohome', function() {
				var defaultTab = document.getElementById("defaultTab");
				//模拟首页点击
				mui.trigger(defaultTab, 'tap');
				//切换选项卡高亮
				var current = document.querySelector(".mui-bar-tab>.mui-tab-item.mui-active");
				if(defaultTab !== current) {
					current.classList.remove('mui-active');
					defaultTab.classList.add('mui-active');
				}
			});

			function push() {
				var options = { cover: false };
				var str = dateToStr(new Date());
				str += ": 大家好啊！我在测试推送";
				plus.push.createMessage(str, "LocalMSG", options);
				if(plus.os.name == "iOS") {
					mui.toast('*如果无法创建消息，请到"设置"->"通知"中配置应用在通知中心显示!');
				}
				pushClick();
			}
			/**
			 *格式化日期时间字符串，格式为"YYYY-MM-DD HH:MM:SS" 
			 * @param {Object} d 时间
			 */
			function dateToStr(d) {
				return(d.getFullYear() + "-" + ultZeroize(d.getMonth() + 1) + "-" + ultZeroize(d.getDate()) + " " + ultZeroize(d.getHours()) + ":" + ultZeroize(d.getMinutes()) + ":" + ultZeroize(d.getSeconds()));
			}
			/**
			 * 长度不够就补零 
			 * @param {Object} v 值
			 * @param {Object} l
			 */
			function ultZeroize(v, l) {
				var z = "";
				l = l || 2;
				v = String(v);
				for(var i = 0; i < l - v.length; i++) {
					z += "0";
				}
				return z + v;
			};

			function pushClick() {
				// 监听点击消息事件
				plus.push.addEventListener("click", function(msg) {
					// 判断是从本地创建还是离线推送的消息
					switch(msg.payload) {
						case "LocalMSG":
							mui.toast("点击本地创建消息启动：" + msg.content);
							break;
						default:
							mui.toast("点击离线推送消息启动：" + msg.content);
							break;
					}
					// 提示点击的内容
					mui.toast(msg.content);
				}, false);
				// 监听在线消息事件
				plus.push.addEventListener("receive", function(msg) {
					if(msg.aps) { // Apple APNS message
						mui.toast("接收到在线APNS消息：" + msg.content);
					} else {
						mui.toast("接收到在线透传消息：" + msg.content);
					}
				}, false);
			}
			document.getElementById('saoyisao').addEventListener('tap', function(e) {
				var newPage = plus.webview.create('barcode_scan.html', 'barcode_scan.html', {
					top: "0px",
					bottom: "0px"
				});
				newPage.show();
			});
		</script>
	</body>

</html>